﻿using Authorization.Center.Controllers;
using Authorization.Center.SqlServer;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S.Transfer.API.Application.Command.TransferCommand;
using S.Transfer.API.Dto;
using S.Transfer.Domain.Model;
using Yitter.IdGenerator;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace S.Transfer.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TransFerController : BaseController
    {
        private readonly IMediator mediator;
        public TransFerController(IMediator _mediator,MySqlDbContext db):base(db)
        {
            this.mediator = _mediator;
        }
        [Authorize]
        [HttpGet("Name")]
        public IActionResult Name()
        {
            return Ok(UserStrategyDto.UserTrueName);
        }
        /// <summary>
        /// 转舍添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost("CreateSheepTransFer")]
        public async Task<IActionResult> CreateSheepTransFer(TransferCreatelCommond command)
        {
            command.CreateBy = UserStrategyDto.UserTrueName;
            return Ok(await mediator.Send(command));
        }
        /// <summary>
        /// 转舍删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        //[HttpDelete("DeleteSheepTransFer")]
        //public async Task<IActionResult> DeleteSheepTransFer(string EarTag)
        //{
        //    return Ok(await mediator.Send(new TransferByIdCommand() { EarTag = EarTag }));
        //}
        /// <summary>
        /// 转舍删除（对外）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete("DeleteSheepTransFer")]
        public async Task<IActionResult> DeleteSheepTransFer(string? EarTag,DateTime? SurrenderDate)
        {
            return Ok(await mediator.Send(new TransferByIdEarCommand() { EarTag = EarTag , SurrenderDate = SurrenderDate }));
        }
        /// <summary>
        /// 转舍删除（自己）
        /// </summary>
        /// <param name="EarTag"></param>
        /// <param name="SurrenderDate"></param>
        /// <returns></returns>
        [HttpDelete("DeleteSheepTransFerMe")]
        public async Task<IActionResult> DeleteSheepTransFerMe(string? EarTag,DateTime? SurrenderDate)
        {
            return Ok(await mediator.Send(new TransferByTypeEarCommand() { EarTag = EarTag , SurrenderDate = SurrenderDate }));
        }
        /// <summary>
        /// 转舍批量添加(对外接口)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost("CreateSheepTransFers")]
        public async Task<IActionResult> CreateSheepTransFers(CreateTranFerCommand command)
        {
            foreach (var item in command.sheep)
            {
                item.CreateDate = DateTime.Now;
                item.DocumentNumbe= YitIdHelper.NextId();
                item.CreateBy = UserStrategyDto.UserTrueName;
                item.TransFerType = 0;
            }
            return Ok(await mediator.Send(command));
        }
        /// <summary>
        /// 转舍批量添加(自己专用)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost("MyCreateSheepTransFers")]
        public async Task<IActionResult> MyCreateSheepTransFers(MyCreateTranFerCommand command)
        {
            foreach (var item in command.sheep)
            {
                item.CreateDate = DateTime.Now;
                item.DocumentNumbe = YitIdHelper.NextId();
                item.CreateBy = UserStrategyDto.UserTrueName;
                item.TransFerType =1;
                
            }
            return Ok(await mediator.Send(command));
        }
        /// <summary>
        /// 转舍修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut("UpdateSheepTransFers")]
        public async Task<IActionResult> UpdateSheepTransFers(TransFerUpdateCommand command)
        {
            command.UpdateBy = UserStrategyDto.UserTrueName;
            return Ok(await mediator.Send(command));
        }
        /// <summary>
        /// 转舍批量删除（自用）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete("TransFerDeleteMore")]
        public async Task<IActionResult> TransFerDeleteMore(string Ids)
        {
            return Ok(await mediator.Send(new TransFerDeleteMore() { Ids = Ids }));
        }
    }
}
